package com.noah.bee.core.util;

import com.alibaba.fastjson.JSONObject;
import com.thetransactioncompany.jsonrpc2.JSONRPC2ParseException;
import com.thetransactioncompany.jsonrpc2.JSONRPC2Response;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by xdw9486 on 2016/7/12.
 * 封装解密方法
 */
public class DecryptByDataBizUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(DecryptByDataBizUtil.class);

    public static String decryptStr(String str, String dataBizApiHost) {

        if (StringUtils.isEmpty(str)) {
            return str;
        }
        Map<String, Object> payResult = new HashMap<>();
        payResult.put("str", str);
        String bizId = String.valueOf(System.currentTimeMillis());
        String jsonString = JsonRPC2Util.generateMsg(bizId, "user.query.decryption", payResult);
        Map<String, Object> paramMap = JSONObject.parseObject(jsonString);
        String response = null;// 用map和javaBean都可以
        try {
            response = (String) HttpUtils.callJsonRpcPost(dataBizApiHost, String.class, paramMap);
        } catch (Exception e) {
            LOGGER.error("调用dataBiz error", e);
        }
//        LoggerUtil.info("调用dataBiz解密返回结果为：{}", response);
        LOGGER.info("调用dataBiz解密返回【{}】结果", response);
        JSONRPC2Response jsonRPC2Response;
        try {
            jsonRPC2Response = JsonRPC2Util.parseResponseMsg(response);
        } catch (JSONRPC2ParseException e) {
            LOGGER.error("解密串{}失败，抛出异常终止服务，{}", str, e.getMessage());
            throw new RuntimeException("解密串" + str + "失败，抛出异常终止服务");
        }
        String decryption = jsonRPC2Response != null ? (String) jsonRPC2Response.getResult() : null;
        LOGGER.info("[{}] 解密后的内容为：{}", str, decryption);
        return decryption;
    }


    public static String encryptStr(String str, String dataBizApiHost) {

        if (StringUtils.isEmpty(str)) {
            return str;
        }
        Map<String, Object> payResult = new HashMap<>();
        payResult.put("str", str);
        String bizId = String.valueOf(System.currentTimeMillis());
        String jsonString = JsonRPC2Util.generateMsg(bizId, "user.query.encryption", payResult);
        Map<String, Object> paramMap = JSONObject.parseObject(jsonString);
        String response = null;// 用map和javaBean都可以
        try {
            response = (String) HttpUtils.callJsonRpcPost(dataBizApiHost, String.class, paramMap);
        } catch (Exception e) {
            LOGGER.error("调用dataBiz error", e);
        }
        LOGGER.info("调用dataBiz加密返回结果为：{}", response);
        JSONRPC2Response jsonRPC2Response;
        try {
            jsonRPC2Response = JsonRPC2Util.parseResponseMsg(response);
        } catch (JSONRPC2ParseException e) {
            LOGGER.error("加密串{}失败，抛出异常终止服务，{}", str, e.getMessage());
            throw new RuntimeException("加密串" + str + "失败，抛出异常终止服务");
        }
        String decryption = jsonRPC2Response != null ? (String) jsonRPC2Response.getResult() : null;
        LOGGER.info("[{}] 加密后的内容为：{}", str, decryption);
        return decryption;
    }
}
